package com.ecy.quadratic.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ecy.quadratic.R;
import com.ecy.quadratic.beans.SectionBean;
import com.ecy.quadratic.beans.Sections;

import java.util.List;

public class SectionAdapter extends BaseAdapter {

    private List<SectionBean> mSectionList;
    private Context mContext;
    private int mCount = 0;

    public SectionAdapter(Context context, Sections sectionList) {
        mContext = context;
        mSectionList = sectionList;
    }

    public void updateData(Sections sections) {
        mSectionList = sections;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        mCount = mSectionList == null ? 0 : mSectionList.size();

        return mCount;
    }

    @Override
    public SectionBean getItem(int position) {
        if (mSectionList != null) {
            return mSectionList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_gridview_section, null);
            viewHolder = new ViewHolder();
            viewHolder.titleTV = (TextView) convertView.findViewById(R.id.comic_section_title_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        SectionBean sectionBean = getItem(position);
        viewHolder.titleTV.setText(sectionBean.getChapter_name());
        return convertView;
    }

    public class ViewHolder {
        public TextView titleTV;
    }

}
